// 导入数据库操作模块
const db = require('../db/index')
// uuid
const uuid = require('uuid')

//
// 创建新的组
exports.createGroup = (req, res) => {
  const { id } = req.user
  const { groupName } = req.body

  const obj = {
    id: uuid.v4(),
    master_id: id,
    name: groupName,
    create_time: new Date(),
  }

  const sql = 'insert into groupes set ?'
  db.query(sql, obj, (error, results) => {
    if (error) return res.cc(error)
    if (results.affectedRows !== 1) return res.cc('创建失败！')
    return res.cc('创建新组成功！', 200)
  })
}

//
// 修改组名
exports.updateGroup = (req, res) => {
  const { id } = req.user
  const { groupName, groupId } = req.body

  const sql = 'update groupes set name=? where id=? and master_id=?'
  db.query(sql, [groupName, groupId, id], (error, results) => {
    if (error) return res.cc(error)
    if (results.affectedRows !== 1) return res.cc('修改失败！')
    return res.cc('修改成功！', 200)
  })
}

//
// 删除组
exports.deleteGroup = (req, res) => {
  const { id } = req.user
  const { groupId } = req.query

  const sql = 'update groupes set is_delete=1 where id=? and master_id=?'
  db.query(sql, [groupId, id], (error, results) => {
    if (error) return res.cc(error)
    if (results.affectedRows !== 1) return res.cc('删除失败！')
    return res.cc('删除成功！', 200)
  })
}

//
// 获取组
exports.getGroup = (req, res) => {
  const { id } = req.user

  const sql = 'select * from groupes where master_id=? and is_delete=0'
  db.query(sql, [id], (error, results) => {
    if (error) return res.cc(error)

    return res.send({
      code: 200,
      message: '获取组的信息成功',
      data: results,
    })
  })
}

//
// 分页获取组中的图片
exports.getImages = (req, res) => {
  const { id: masterId } = req.user
  const { page, pageSize, groupId } = req.query

  // let a = `select * from image where master_id=? and (group_id=? or group_id is NULL) and is_delete=0 order by upload_time desc limit ?,?`

  let total = ''
  if (groupId) {
    const countSql = `select count(*) as count from image where master_id=? and group_id=?
    and is_delete=0`
    db.query(countSql, [masterId, groupId], (err, results) => {
      if (err) return res.cc(err)
      total = results[0].count
    })

    const sql = `select * from image where master_id=? and group_id=?
    and is_delete=0 order by upload_time desc limit ?,?`
    db.query(
      sql,
      [masterId, groupId, (page - 1) * pageSize, pageSize],
      (error, results) => {
        if (error) return res.cc(error)

        return res.send({
          code: 200,
          message: '获取组中的图片成功',
          data: { list: results, total },
        })
      }
    )
  } else {
    const countSql = `select count(*) as count from image where master_id=? and is_delete=0`
    db.query(countSql, [masterId], (err, results) => {
      if (err) return res.cc(err)
      total = results[0].count
    })

    const sql = `select * from image where master_id=? 
    and is_delete=0 order by upload_time desc limit ?,?`
    db.query(
      sql,
      [masterId, (page - 1) * pageSize, pageSize],
      (error, results) => {
        if (error) return res.cc(error)

        return res.send({
          code: 200,
          message: '获取组中的图片成功',
          data: { list: results, total },
        })
      }
    )
  }
}

//
// 删除图片
exports.deleteImage = (req, res) => {
  const { id: masterId } = req.user
  const { imageId } = req.query
  const time = new Date()

  const sql = `update image set is_delete=1, delete_time=? where id=? and master_id=?`
  db.query(sql, [time, imageId, masterId], (error, results) => {
    if (error) return res.cc(error)
    if (results.affectedRows !== 1) return res.cc('删除失败')

    return res.cc('删除成功！', 200)
  })
}

//
// 恢复图片
exports.recoverImage = (req, res) => {
  const { id: masterId } = req.user
  const { imageId } = req.body
  const time = new Date()

  const sql = `update image set is_delete=0, upload_time=? where id=? and master_id=?`
  db.query(sql, [time, imageId, masterId], (error, results) => {
    if (error) return res.cc(error)
    if (results.affectedRows !== 1) return res.cc('恢复失败')

    return res.cc('恢复成功！', 200)
  })
}

//
// 分页获取删除的图片
exports.getDeleteImages = (req, res) => {
  const { id: masterId } = req.user
  const { page = 1, pageSize = 10 } = req.query

  let total = ''
  const sql0 =
    'select count(*) as total from image where is_delete=1 and master_id=? '
  db.query(sql0, [masterId], (error, results) => {
    if (error) return res.cc(error)
    total = results[0].total
  })

  const sql = `select * from image where is_delete=1 and master_id=? order by delete_time desc limit ?, ?`
  db.query(
    sql,
    [masterId, (page - 1) * pageSize, pageSize],
    (error, results) => {
      if (error) return res.cc(error)

      return res.send({
        code: 200,
        data: { list: results, total },
        message: '获取成功！',
      })
    }
  )
}
